<template>
  <el-container class="layout-container">
    <el-aside width="220px" class="aside">
      <div class="logo">
        <img src="../assets/logo.svg" alt="Logo" />
        <span style="font-size: smaller;">美遇校园二手商品交易平台</span>
      </div>
      <el-scrollbar>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#fff"
        >
          <el-menu-item index="1"  @click="select">
            <el-icon><House /></el-icon>
          <span>首页</span>
        </el-menu-item>
        <el-sub-menu index="2">
          <template #title>
            <el-icon><OfficeBuilding /></el-icon>
            <span> 商品管理</span>
          </template>
            <el-menu-item index="2-1" @click="handleChooseProduct">商品信息</el-menu-item>
            <el-menu-item index="2-2" @click="propertiesApartment">商品分类</el-menu-item>
            <el-menu-item index="2-3" @click="messageApartment">商品属性</el-menu-item>
            <el-menu-item index="2-4" @click="handleChooseRoom">品牌管理</el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="3">
          <template #title>
            <el-icon><Wallet /></el-icon>
            <span>订单管理</span>
        </template>
        <el-menu-item index="3-1" @click="leaseManagement">订单信息</el-menu-item>
        <el-menu-item index="3-2" @click = "lookRoom">退货申请</el-menu-item>
        <el-menu-item index="3-3" @click="returnRecord">退货记录</el-menu-item>
        <el-menu-item index="3-4" @click="returnReasonSetting">退货原因设置</el-menu-item>
        </el-sub-menu>
        <el-sub-menu index="4">
          <template #title>
            <el-icon><Tools /></el-icon>
            <span>信息管理</span>
        </template>
        <el-menu-item index="4-1" @click="userList">用户列表 </el-menu-item>
          <el-menu-item index="4-2" @click="systemUser">管理员列表</el-menu-item>
        </el-sub-menu>
          <el-menu-item index="5"  @click="selectReview">
            <el-icon><ChatLineRound /></el-icon>
          <span>留言管理</span>
        </el-menu-item>
        </el-menu>
      </el-scrollbar>
    </el-aside>

    <el-container>
      <el-header class="header">
        <div class="header-left">
          <el-icon class="toggle-sidebar" @click="toggleSidebar">
            <Fold v-if="!isCollapse" />
            <Expand v-else />
          </el-icon>

        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="user-info">
              <el-avatar :size="32" :src="userAvatar" />
              <span class="username">{{ username }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="goToPersonalInfo">个人信息</el-dropdown-item>
                <el-dropdown-item>修改密码</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main class="main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import { Menu as Fold, Expand, House, Tools, OfficeBuilding, Wallet, ChatLineRound } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import instance from '../utils/request'

const router = useRouter()
const isCollapse = ref(false)
const activeMenu = ref('1')
const username = ref(sessionStorage.getItem("adminName") || '管理员')
// 在script部分
const userAvatar = ref('') // 从API获取或使用相对路径

 function select() {
  router.push('/layoutSystem/visualizationView')
}
 function messageApartment() {

  router.push('/layoutSystem/productTypeView')
}

 function propertiesApartment() {
  router.push('/layoutSystem/categoryView')
}
function handleChooseRoom (){
router.push('/layoutSystem/brandManagementView')
}
function handleChooseProduct (){
router.push('/layoutSystem/productView')
}
function systemUser (){
router.push('/layoutSystem/adminView')
}
function goToPersonalInfo() {
  router.push('/layoutSystem/personalInformation')
}
function returnRecord() {
  router.push('/layoutSystem/returnRecordView')
}
function returnReasonSetting() {
  router.push('/layoutSystem/reasonForReturnView')
}
function selectReview() {
  router.push('/layoutSystem/messageView')
}

onMounted(() => {
  select()
  if(router.currentRoute.value.path === '/'){
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  }
  getUserInfo() // 获取用户信息
})
//获取用户信息
const getUserInfo = async() => {
  const adminId = sessionStorage.getItem("adminId");
  const res = await instance.get('/user/getAdminInfo',{
    params: {
      id: adminId
    }
  })
  if (res.data.code === 200) {
    username.value = res.data.data.adminName
    userAvatar.value = res.data.data.adminProfilePictureSrc || '/uploads/default-avatar.png'; // 设置头像
  }
}

// 监听头像和用户名变化
watch(() => [username.value, userAvatar.value], () => {
  getUserInfo() // 重新获取用户信息
})
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}
const lookRoom = () => {
  router.push('/layoutSystem/returnRequestView')
}
const leaseManagement = () => {
  router.push('/layoutSystem/orderManagementView')
}
const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('username')
  sessionStorage.getItem("adminId")
  sessionStorage.getItem("adminName")
  router.push('/login')
}
const userList = () => {
  router.push('/layoutSystem/userTableView')
}

</script>

<style scoped>
.layout-container {
  height: 100vh;
  background-color: #f0f2f5;
}

.aside {
  background-color: #304156;
  transition: width 0.3s;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px solid #1f2d3d;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 12px;
}

.header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.toggle-sidebar {
  font-size: 20px;
  cursor: pointer;
  color: #666;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
  font-size: 14px;
  color: #666;
}

.main {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-menu {
  border-right: none;
}

:deep(.el-menu-item) {
  &:hover {
    background-color: #263445 !important;
  }
  &.is-active {
    background-color: #1890ff !important;
  }
}
</style>
